package com.soilflow.know.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.*;
import com.soilflow.common.utils.bean.BeanUtils;
import com.soilflow.know.mapper.GamesInfoMapper;
import com.soilflow.know.mapper.GamesScoreHistoryMapper;
import com.soilflow.know.service.GamesInfoService;
import com.soilflow.know.service.GamesScoreHistoryService;
import com.soilflow.know.service.GamesUserScoreService;
import com.soilflow.system.service.ISysUserReceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class GamesScoreHistoryServiceImpl extends ServiceImpl<GamesScoreHistoryMapper, GamesScoreHistory> implements GamesScoreHistoryService {

    @Autowired
    private GamesUserScoreService gamesUserScoreService;

    @Autowired
    private ISysUserReceptionService sysUserReceptionService;

    @Override
    public GamesScoreHistory selectGamesScoreHistoryById(Long id) {
        GamesScoreHistory landInfoVos = this.baseMapper.selectGamesScoreHistoryById(id);
        return landInfoVos;
    }


    @Override
    public List<GamesScoreHistory> selectGamesScoreHistoryList(GamesScoreHistory dto) {
        List<GamesScoreHistory> landInfoVos = this.baseMapper.selectGamesScoreHistoryList(dto);
        return landInfoVos;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertGamesScoreHistory(GamesScoreHistory dto) {
        boolean save =false;
        GamesScoreHistory landInfo = new GamesScoreHistory();
        BeanUtils.copyProperties(dto,landInfo);
        save = this.save(landInfo);
        return save;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGamesScoreHistory(GamesScoreHistory dto) {
        boolean save =false;
        GamesScoreHistory landInfo = new GamesScoreHistory();
        BeanUtils.copyProperties(dto,landInfo);
        //修改百事通信息
        save = this.updateById(landInfo);
        return save;
    }

    @Override
    public Boolean deleteGamesScoreHistoryByIds(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Transactional
    @Override
    public GamesZhuangZhangVo zhuangZhang(GamesZhuangZhangVo vo){

        // 第一步：校验余额
        GamesUserScore fromUserVo = gamesUserScoreService.selectGamesUserScoreByUserId(vo.getFromUserId());
        if(fromUserVo.getScore().doubleValue() < vo.getAmount().doubleValue()){
            vo.setCode(500);
            vo.setMsg("用户积分不足");
            return vo;
        }

        // 第二步：修改2位用户的余额
        SysUserReception toUserInfo = sysUserReceptionService.selectUserByUserName(vo.getToUserName());
        if(toUserInfo==null){
            vo.setCode(500);
            vo.setMsg("对方账号不存在");
            return vo;
        }

       if( vo.getFromUserId() == toUserInfo.getUserId()){
           vo.setCode(500);
           vo.setMsg("无法向自己的账号转账");
           return vo;
       }

        GamesUserScore fromUpdate = new GamesUserScore();
        fromUpdate.setUserId(fromUserVo.getUserId());
        fromUpdate.setUserName(fromUserVo.getUserName());
        fromUpdate.setScore(vo.getAmount());
        gamesUserScoreService.delGamesUserScoreOnly(fromUpdate);

        GamesUserScore toUpdate = new GamesUserScore();
        toUpdate.setUserId(toUserInfo.getUserId());
        toUpdate.setUserName(toUserInfo.getUserName());
        toUpdate.setScore(vo.getAmount());
        gamesUserScoreService.addGamesUserScoreOnly(toUpdate);

        // 第二步：增加加分记录
        final GamesUserScore addUser = gamesUserScoreService.selectGamesUserScoreByUserId(toUserInfo.getUserId());
        GamesScoreHistory addScoreVo = new GamesScoreHistory();
        addScoreVo.setOrigin("转账");
        addScoreVo.setRemark("来自"+fromUserVo.getUserName()+"转账");
        addScoreVo.setUserId(addUser.getUserId());
        addScoreVo.setType("0");
        addScoreVo.setScoreTime(new Date());
        addScoreVo.setScore(vo.getAmount());
        addScoreVo.setUserScore(addUser.getScore());
        addScoreVo.setUserName(fromUserVo.getUserName());
        this.save(addScoreVo);

        // 第三步：增加减分记录
        final GamesUserScore delUser = gamesUserScoreService.selectGamesUserScoreByUserId(vo.getFromUserId());
        GamesScoreHistory delScoreVo = new GamesScoreHistory();
        delScoreVo.setOrigin("转账");
        delScoreVo.setRemark("向"+toUserInfo.getUserName()+"转账");
        delScoreVo.setUserId(delUser.getUserId());
        delScoreVo.setType("0");
        delScoreVo.setScoreTime(new Date());
        delScoreVo.setScore(vo.getAmount());
        delScoreVo.setUserScore(delUser.getScore());
        delScoreVo.setUserName(toUserInfo.getUserName());
        this.save(delScoreVo);

        vo.setCode(200);
        return vo;
    }

}
